﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triangles
{
    class Triangle : IEquatable<Triangle>, IComparable<Triangle>
    {
        public const int SidesCount = 3;
        #region Ctor
        public Triangle()
        {

        }
        public Triangle(string name)
        {
            Name = name;
        }

        public Triangle(double sideA, double sideB, double sideC)
        {
            SideA = sideA;
            SideB = sideB;
            SideC = sideC;
        }
        #endregion

        #region Fields
        private double[] mSides = { 1, 1, 1 };
        private bool mIsCorrect = true;
        #endregion

        #region Properties
        public string Name { get; set; }
        public double SideA
        {
            get { return mSides[0]; }
            set 
            {
                if (value <= 0)
                {
                    IsCorrect = false;
                }
                mSides[0] = value; 
            }
        }

        public double SideB
        {
            get { return mSides[1]; }
            set 
            {
                if (value <= 0)
                {
                    IsCorrect = false;
                }
                mSides[1] = value; 
            }
        }

        public double SideC
        {
            get { return mSides[2]; }
            set 
            {
                if (value <= 0)
                {
                    IsCorrect = false;
                }
                mSides[2] = value; 
            }
        }

        public bool IsCorrect
        {
            get 
            {
                mIsCorrect = SquarePower2 > 0;
                return mIsCorrect; 
            }
            set
            {
                mIsCorrect = value;
            }
        }

        public double Perimeter
        {
            get
            {
                double perimeter = SideA + SideB + SideC;
                return perimeter;
            }
        }

        public double SquarePower2
        {
            get
            {
                double s = Perimeter / 2;
                double square = s * (s - SideA) * (s - SideB) * (s - SideC);
                return square;
            }
        }

        public double Square
        {
            get 
            {
                if (IsCorrect)
                {
                    return Math.Sqrt(SquarePower2); 
                }
                else
                {
                    return 0;
                }
            }
        }

        public double this[int index]
        {
            get
            {
                return mSides[index];
            }
            set
            {
                mSides[index] = value;
            }
        }
        #endregion

        public override string ToString()
        {
            return string.Format("Triangle {0}: Square {1}", Name, Square);
        }

        public bool Equals(Triangle other)
        {
            if (other == null)
	        {
		        throw new NullReferenceException();
	        }
            return other.Square.Equals(Square);
        }

        public int CompareTo(Triangle other)
        {
            if (other == null || !other.IsCorrect) return 1;
            return SquarePower2.CompareTo(other.SquarePower2);
        }
    }
}
